/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sacodemel.financeiro.mb;

import com.sacodemel.financeiro.servico.IConsoleService;
import com.sacodemel.financeiro.servico.impl.ConsoleService;
import com.sacodemel.financeiro.util.JSFUtil;
import java.util.Iterator;
import java.util.List;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlColumn;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author X4Q1
 */
@ManagedBean
@RequestScoped
public class ConsoleMB {

    private String sql;
    private String tipoComando;
    private String entidade;
    private IConsoleService consoleService = new ConsoleService();
    private DataModel lista;
    private HtmlPanelGroup tabelaDinamica; // Placeholder.


    public String executar() {
        
        try{
            setLista(new ListDataModel(consoleService.executarComandoSQL(tipoComando, entidade, sql)));
            
            this. popularTabelaDinamica();
//            
//            Iterator iterator = lista.iterator();
//            
//            
//        while(iterator. hasNext()){        
//            System.out.println(iterator.next());    
//
//        }
        
            JSFUtil.adicionaMensagemSucesso();
        }catch(Exception e){
            JSFUtil.adicionaMensagemFalha(e.getMessage());
        }
        
        return null;
    }
    
    
     private void popularTabelaDinamica() {

        // Create <h:dataTable value="#{myBean.dynamicList}" var="dynamicItem">.
        HtmlDataTable dynamicDataTable = new HtmlDataTable();
        dynamicDataTable.setValueExpression("value", createValueExpression("#{consoleMB.lista}", List.class));
        dynamicDataTable.setVar("dynamicItem");

        // Iterate over columns.
        for (int i = 0; i < getLista().getRowCount(); i++) {

            // Create <h:column>.
            HtmlColumn column = new HtmlColumn();
            dynamicDataTable.getChildren().add(column);

            // Create <h:outputText value="dynamicHeaders[i]"> for <f:facet name="header"> of column.
            HtmlOutputText header = new HtmlOutputText();
            header.setValue("-");
            column.setHeader(header);

            // Create <h:outputText value="#{dynamicItem[" + i + "]}"> for the body of column.
            HtmlOutputText output = new HtmlOutputText();
            output.setValueExpression("value",
                createValueExpression("#{dynamicItem[" + i + "]}", String.class));
            column.getChildren().add(output);
        }

        // Add the datatable to <h:panelGroup binding="#{myBean.dynamicDataTableGroup}">.
        tabelaDinamica = new HtmlPanelGroup();
        tabelaDinamica.getChildren().add(dynamicDataTable);
    }

    

    /**
     * @return the sql
     */
    public String getSql() {
        return sql;
    }

    /**
     * @param sql the sql to set
     */
    public void setSql(String sql) {
        this.sql = sql;
    }

    /**
     * @return the consoleService
     */
    public IConsoleService getConsoleService() {
        return consoleService;
    }

    /**
     * @param consoleService the consoleService to set
     */
    public void setConsoleService(IConsoleService consoleService) {
        this.consoleService = consoleService;
    }

    /**
     * @return the tipoComando
     */
    public String getTipoComando() {
        return tipoComando;
    }

    /**
     * @param tipoComando the tipoComando to set
     */
    public void setTipoComando(String tipoComando) {
        this.tipoComando = tipoComando;
    }

    /**
     * @return the entidade
     */
    public String getEntidade() {
        return entidade;
    }

    /**
     * @param entidade the entidade to set
     */
    public void setEntidade(String entidade) {
        this.entidade = entidade;
    }

    /**
     * @return the tabelaDinamica
     */
    public HtmlPanelGroup getTabelaDinamica() {
        return tabelaDinamica;
    }

    /**
     * @param tabelaDinamica the tabelaDinamica to set
     */
    public void setTabelaDinamica(HtmlPanelGroup tabelaDinamica) {
        this.tabelaDinamica = tabelaDinamica;
    }
    
    private ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createValueExpression(
            facesContext.getELContext(), valueExpression, valueType);
    }

    /**
     * @return the lista
     */
    public DataModel getLista() {
        return lista;
    }

    /**
     * @param lista the lista to set
     */
    public void setLista(DataModel lista) {
        this.lista = lista;
    }


}
